<?php

import("data.db.DbTable");
import("data.db.DbFactory");
import("data.logic.catalog.LFileStorage");

class DbFile extends DbTable {
	
	const TB_TABLENAME	= "files";
	const TB_I			= 'fl';
	
	const TB_NAME		= 'fl_name';
	const TB_SYSNAME	= 'fl_name_gen';
	const TB_DESCRIPTION = 'fl_description';
	const TB_STATUS		= 'fl_status';
	
	const STATUS_LOCAL_STORED = 0;
	const STATUS_LOCAL_REMOTE_STORED = 1;
	const STATUS_LOCAL_CACHE = 2;
	const STATUS_LOCAL_DELETED = 3;
	const STATUS_LOCAL_REMOTE_DELETED = 4;
	
	function _get_TABLENAME() {return self::TB_TABLENAME;}
	function _get_ID() {return self::TB_I."_id";}
	function _get_SUFFIX() {return self::TB_I;}
	
	// properties
	var $name;
	var $sysname;
	var $description;
	var $status;
	
	function DbFile() {
		parent::DbTable();
		$this->sysname = $this->generateSysName();
		$this->status = self::STATUS_LOCAL_STORED;
	}
	
	function getFile() {
		return new File(UPLOAD_PATH . "/" . $this->getSysName());
	}
	
	function setName($name) {$this->name = $name;}
	function getName() {return $this->name;}
	
	//function setSysName($name) {$this->sysname = $name;}
	function getSysName() {return $this->sysname;}
	
	function setDescription($des) {$this->description = $des;}
	function getDescription() {return $this->description;}
	
	function setStatus($ul) {$this->status = $ul;}
	function getStatus() {return $this->status;}
	
	
	function setFileStorages($storages) {
		$f = $this->getBackwardForeign('data.logic.catalog.LFileStorage');
		$f->setList($storages);
	}
	function getFileStorages() {
		$f = $this->getBackwardForeign('data.logic.catalog.LFileStorage');
		return $f->getList();
	}
	
	function setCategory(&$category) {
		$this->setForwardForeign('data.logic.catalog.LCategory', $category);
	}
	function getCategory() {
		$f = $this->getForwardForeign('data.logic.catalog.LCategory');
		return $f->getForeign();
	}
	function getCategoryName() {
		$cat = $this->getCategory();
		return ($cat?$cat->getName():"not defined");
	}
	
	function setUser(&$user) {
		$this->setForwardForeign('data.logic.user.LUser', $user);
	}
	function getUser() {
		$f = $this->getForwardForeign('data.logic.user.LUser');
		return $f->getForeign();
	}
	function getUserName() {
		$u = $this->getUser();
		return $u->getName();
	}
	
	function generateSysName() {
		return md5(uniqid("sysfile"))."_".md5(uniqid("sysfilepart")).".dat";
	}
	
	// restore
	function fillEntity($arrResult) {
		parent::fillEntity($arrResult);
		$this->setName($arrResult[self::TB_NAME]);
		$this->sysname = $arrResult[self::TB_SYSNAME];
		$this->setDescription($arrResult[self::TB_DESCRIPTION]);
		$this->setStatus($arrResult[self::TB_STATUS]);
		
		//Forward Foreigns
		$this->initForeign($arrResult, 'data.logic.catalog.LCategory');
		$this->initForeign($arrResult, 'data.logic.user.LUser');
	}
	
	//store
	function fillFields() {
		parent::fillFields();
		$this->fildsForStore[self::TB_NAME] = "'".$this->getName()."'";
		$this->fildsForStore[self::TB_SYSNAME] = "'".$this->getSysName()."'";
		$this->fildsForStore[self::TB_DESCRIPTION] = "'".$this->getDescription()."'";
		$this->fildsForStore[self::TB_STATUS] = $this->getStatus();
		
		$this->fildsAddForeign('data.logic.catalog.LCategory');
		$this->fildsAddForeign('data.logic.user.LUser');
	}
	
	// static find
	function findByName($name) {
		return parent::__selectByCriteria(
			self::TB_NAME."='".$name."' and ".self::_getUserCriteria(),
			'data.logic.catalog.LFile');
	}
	
	function findByLevel($level) {
		$list = parent::__selectAggregate("select * from ".self::_get_TABLENAME()." having (select count(*) from ".
			LFileStorage::_get_TABLENAME()." where ".DbTable::_get_FOREIGN('data.logic.catalog.LFileStorage','data.logic.catalog.LFile')."=".self::_get_ID().")=".$level);
		if (count($list)>0) {
			return DbFactory::makeObject('data.logic.catalog.LFile', $list[0]);
		}
		return false;
	}
	
	function findBySysName($name) {
		return parent::__selectByCriteria(
			self::TB_NAME."='".$name."' and ".self::_getUserCriteria(),
			'data.logic.catalog.LFile');
	}
	
	function findAll() {
		return parent::__selectAllByCriteria(
			self::_getUserCriteria()." and ".self::_getStatusCriteria(), 
			'data.logic.catalog.LFile');
	}
	
	function _getUserCriteria() {
		global $_SESSION;
		$user = $_SESSION[LUser::KEY];
		return DbTable::_get_FOREIGN('data.logic.catalog.LFile','data.logic.user.LUser').'='.$user->getId();
	}
	
	function _getStatusCriteria() {
		return self::TB_STATUS."<>".self::STATUS_LOCAL_DELETED.
			" and ".self::TB_STATUS."<>".self::STATUS_LOCAL_REMOTE_DELETED;
	}
	
	// logic
	function getRating() {
		$list = $this->getFileStorages();
		$sum = count($list);
		/*if (count($list)==0) return 0;
		$sum = 0;
		foreach ($list as $rs) {
			$sum += $rs->getRating();
		}*/
		//return $sum/count($list);
		return $sum;
	}
	
	function getStorageCount() {
		$list = $this->getFileStorages();
		return count($list);
	}
}